package com.zingrow.web.InformationDistribute.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.zingrow.common.utils.MongoHelper;
import com.zingrow.web.InformationDistribute.dao.InformationDistributeDao;
import com.zingrow.web.entity.InformationDistribute;

@Repository
public class InformationDistributeDaoImpl implements InformationDistributeDao {
    MongoHelper mongo = MongoHelper.getInstance();

    /**
     * 当个方案初始化10条数据，时间戳正序
     */
    @Override
    public List<InformationDistribute> queryInitById(Class<?> entity, String ifmid) {
        return mongo.queryInitById(InformationDistribute.class, ifmid);
    }

    /**
     * 单个方案ID 时间戳 参数 正序排序查询10条数
     */
    @Override
    public List<InformationDistribute> queryByIDlastTimestamp(Class<?> entity, String ifmid, Long timestamp) {
        return mongo.queryByIDlastTimestamp(InformationDistribute.class, ifmid, timestamp);
    }

    /**
     * 根据文章ID获取相似信息
     */
    @Override
    public List<InformationDistribute> queryLikeByGroupId(Class<?> entity,String ifmId, String groupId,Long timestamp) {
        List<InformationDistribute> list= mongo.queryLikeByGroupId(InformationDistribute.class, ifmId,groupId,timestamp);
        return list;
              
    }

    @Override
    public InformationDistribute queryContentByArticleId(Class<?> entity, String articleId,String ifmId) {
        return mongo.queryContentByArticleId(InformationDistribute.class, articleId,ifmId);
    }
    
    @Override
    public InformationDistribute queryContentById(Class<?> entity, String _id) {
        return mongo.queryContentById(InformationDistribute.class,_id);
    }

    @Override
    public List<InformationDistribute> queryUserEnshrine(Class<?> paramClass,List<String> ifmids, List<String> paramList,Long timestamp) {
        return mongo.queryUserEnshrine(InformationDistribute.class, ifmids,paramList, timestamp);
    }

    @Override
    public List<InformationDistribute> queryKewWorkLike(Class<?> entity, String KewWord, Long timestamp,
            List<String> ifmIds) {
        KewWord=getLikeAppString(KewWord);
        return mongo.queryKewWorkLike(InformationDistribute.class, KewWord, timestamp, ifmIds);
    }
    
    /**
     * 判断是否是中文字符
     * @param codePoint
     * @return
     */
    protected static boolean isChineseChar(int codePoint){
        Character.UnicodeScript sc = Character.UnicodeScript.of(codePoint);
        if (sc == Character.UnicodeScript.HAN) {
            return true;
        }
         return false;
     }

    
    private String getLikeAppString(String keywords)
    {
        char[] chars=keywords.toCharArray();
        StringBuffer sb=new StringBuffer();
        int size=chars.length-1;
        int i=0;
        for(char ch: chars)
        {
            sb.append(ch);
            if(i!=size && isChineseChar(ch))
            {
                sb.append("(<.*?em.*?>){0,1}");
            }
            i++;
        }
        
        return sb.toString();
    }
    
    public static void main(String[] args) {
        InformationDistributeDaoImpl i=new InformationDistributeDaoImpl();
        List<String> list=new ArrayList<String>();
        list.add("307");
        System.out.println(i.queryKewWorkLike(InformationDistribute.class, "绝味", 0l, list));
    }

}
